﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Csla.Data;
using Csla;
using BizControl.Library.BusinessObjects.People;
using BizControl.Library.BusinessObjects.Models;

namespace BizControl.Library.Tests
{
  [TestClass]
  public class PersonBOFixture
  {
    private PersonBO NewPerson()
    {
      var person = PersonBO.NewPerson();
      person.FirstName = "Michael";
      person.LastName = "Baranov";
      person.PasswordHash = "passwordHash";
      person.PasswordSalt = "passwordSalt";
      person.Login = "michael.baranov";
      person.Email = "bla@bla.bla";
      return person;
    }

    [TestMethod]
    public void AddPersonBOTest()
    {
      var person = NewPerson();
      person = person.Save();
      var person2 = PersonBO.GetPerson(person.PersonId);
      Assert.AreEqual(person.PersonId, person2.PersonId);
    }

    [TestMethod]
    public void UpdatePersonBOTest()
    {
      var newPerson = NewPerson();
      newPerson = newPerson.Save();
      var people = PersonList.GetPersonList();
      var personInfo = people.First();
      var person = PersonBO.GetPerson(personInfo.Id);
      person.MiddleName = "Sergeevich";
      person = person.Save();
      var person2 = PersonBO.GetPerson(person.PersonId);
      Assert.AreEqual(person.PersonId, person2.PersonId);
    }

    [TestMethod]
    public void CreatedByPersonBOTest()
    {
      var people = PersonList.GetPersonList();
      var personInfo = people.First();
      var person2 = NewPerson();
      person2.CreatedBy = personInfo;
      person2 = person2.Save();
      Assert.AreEqual(personInfo.Id, PersonBO.GetPerson(person2.PersonId).CreatedBy.Id);
    }

    [TestMethod]
    public void DeletePersonBOTest()
    {
      var newPerson = NewPerson();
      newPerson = newPerson.Save();
      var people = PersonList.GetPersonList();
      var personInfo = people.First();
      var person = PersonBO.GetPerson(personInfo.Id);
      var id = person.PersonId;
      person.Delete();
      person = person.Save();
      person = PersonBO.GetPerson(id);
      Assert.AreEqual(Guid.Empty, person.PersonId);
    }

    [TestMethod]
    public void AddNoteBOTest()
    {
      // login
      var newPerson = NewPerson();
      newPerson = newPerson.Save();
      var people = PersonList.GetPersonList();
      var personInfo = people.First();
      var person = PersonBO.GetPerson(personInfo.Id);
      var notes = person.Notes;
      var note = notes.AddNewNote();
      note.Name = "The First Note";
      var note2 = notes.AddNew();
      note2.AssignToPerson(person);
      note2.Name = "The Second Note";
      person.Save();
    }

    [TestMethod]
    public void AddModelToNewPersonTest()
    {
      // login
      var person = NewPerson();
      var model = ModelBO.NewModel();
      model.Name = "Model #3";
      model = model.Save(); // model should be saved
      person.Models.AddModel(model);
      person = person.Save();
      Assert.IsTrue(PersonBO.GetPerson(person.PersonId).Models.Contains(model.ModelId));
    }
  }
}
